package com.egobuy.product.controller;


import com.egobuy.entity.Item;
import com.egobuy.entity.ItemDesc;
import com.egobuy.pojo.BuyResult;
import com.egobuy.pojo.EgoBuyPager;
import com.egobuy.product.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Hello
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/item")
@CrossOrigin(allowedHeaders = "*",value = "*",origins = "*",methods = {RequestMethod.GET,RequestMethod.POST},maxAge = 3600)
public class ItemController {
    @Autowired
    private IItemService itemService;
    @Autowired
    private RestTemplate restTemplate;

//    @RequestMapping("/query")
//    public List<Item> query(){
//        return itemService.list();
//    }
    @RequestMapping(value = "/queryId",produces = "application/json;chartset=utf-8")
    public Item queryId(long itemId){
        return itemService.getById(itemId);
    }


    @RequestMapping(value = "/query",produces = "application/json;charset=utf-8")
    public BuyResult<EgoBuyPager<Item>> query(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10")  int size, Item item){
        return itemService.queryByPage(page,size,item);
    }
    @PostMapping("/save")
    public BuyResult save(Item item, ItemDesc itemDesc){

        BuyResult result = itemService.save(item, itemDesc);
        
        //页面静态化
        itemService.itemStatic(item.getId());

        // 远程调用search工程同步索引
        restTemplate.getForObject("http://localhost:11011/search-api/search/save?itemId=" + item.getId(), BuyResult.class);
        return result;

    }
    @RequestMapping(value = "/get/{itemId}",produces = "application/json;charset=utf-8")
    public BuyResult get(@PathVariable long itemId){
        return itemService.queryDetail(itemId);
    };
}
